package net.whg.pattern;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HyPattern {

    public static final String regexPattern = "[\\\\s~·`!！√▼①②③④⑤⑥⑦⑧⑨⑩□○G◎■◇⊙@#￥$%^……&*◆⊙★▲●※O☆△\\\\-——\\\\-_\\-=+【\\[\\]】｛{}｝\\\\|、\\\\\\\\；;：:‘'“”\\\"，,《<。.》>、/？?]";

    public static final String DISEASE_CODE_PATTERN = "^[A-Z]{1}[0-9]{2}(\\.[0-9a-zA-Z]){1}.*";


    public static void main(String[] args) {

        if ("R581".matches(DISEASE_CODE_PATTERN)){
            System.out.println(true);
        }else {
            System.out.println(false);
        }

        String[] nameArray = new String[]
                {"{竟安针}丙泊酚中/长链脂肪乳注射液20ml:0.2g","{万汉润舒滴眼液}左氧氟沙星滴眼液5ml:24.4mg",
                        "红霉素软膏1%10g","★{大惠迪森针}注射用头孢呋辛钠1.5g","[贝莱口服溶液]盐酸氨溴索口服溶液100ml:0.6g",
                        "[巴曲亭针]注射用矛头蝮蛇血凝酶2单位","[力月西针]咪达唑仑注射液1ml:5mg","[凯特力吸入剂]吸入用七氟烷1ml",
                        "硫酸阿托品注射液1ml:0.5mg","氯化钠注射液0.9%10ml","地塞米松磷酸钠注射液1ml:5mg",
                        "{}苯磺顺阿曲库铵注射液5ml：10mg","[欧贝针]盐酸昂丹司琼注射液4ml:8mg","注射用盐酸瑞芬太尼1mg",
                        "葡萄糖氯化钠注射液5%250ml","氯化钠注射液0.9%100ml","★[希刻劳干混悬剂]头孢克洛干混悬剂0.125g*6",
                        "枸橼酸舒芬太尼注射液1ml:50ug","[抗坏血酸针]维生素C注射液5ml:1g","西帕依固龈液30ml",
                        "【安内真】苯磺酸氨氯地平片【4+7】5毫克*21片","【拜阿司匹林】阿司匹林肠溶片100毫克*30片","【搏力高】坎地沙坦酯片【4+7】8毫克*28片","艾司唑仑片1毫克*1片",
                        "(集采)(宏菲)甘草酸二铵胶囊(50mg*24粒)(基)","溴隐停片(2.5mg*30片)A(基)","(适利达)拉坦前列素滴眼剂",
                        "{竟安针}丙泊酚中/长链脂肪乳注射液20ml:0.2g","{万汉润舒滴眼液}左氧氟沙星滴眼液5ml:24.4mg","红霉素软膏1%10g","★{大惠迪森针}注射用头孢呋辛钠1.5g",
                        "[贝莱口服溶液]盐酸氨溴索口服溶液100ml:0.6g","[巴曲亭针]注射用矛头蝮蛇血凝酶2单位","[力月西针]咪达唑仑注射液1ml:5mg","[凯特力吸入剂]吸入用七氟烷1ml",
                        "硫酸阿托品注射液1ml:0.5mg","氯化钠注射液0.9%10ml","地塞米松磷酸钠注射液1ml:5mg","{}苯磺顺阿曲库铵注射液5ml：10mg","[欧贝针]盐酸昂丹司琼注射液4ml:8mg",
                        "注射用盐酸瑞芬太尼1mg","葡萄糖氯化钠注射液5%250ml","氯化钠注射液0.9%100ml","★[希刻劳干混悬剂]头孢克洛干混悬剂0.125g*6","枸橼酸舒芬太尼注射液1ml:50ug",
                        "[抗坏血酸针]维生素C注射液5ml:1g","西帕依固龈液30ml"};
        String[] array = new String[] {
                "【安内真】苯磺酸氨氯地平片【4+7】5毫克*21片","【拜阿司匹林】阿司匹林肠溶片100毫克*30片","【搏力高】坎地沙坦酯片【4+7】8毫克*28片","艾司唑仑片1毫克*1片",
                "(集采)(宏菲)甘草酸二铵胶囊(50mg*24粒)(基)","溴隐停片(2.5mg*30片)A(基)","(适利达)拉坦前列素滴眼剂",
                "{竟安针}丙泊酚中/长链脂肪乳注射液20ml:0.2g","{万汉润舒滴眼液}左氧氟沙星滴眼液5ml:24.4mg","红霉素软膏1%10g","★{大惠迪森针}注射用头孢呋辛钠1.5g",
                "[贝莱口服溶液]盐酸氨溴索口服溶液100ml:0.6g","[巴曲亭针]注射用矛头蝮蛇血凝酶2单位","[力月西针]咪达唑仑注射液1ml:5mg","[凯特力吸入剂]吸入用七氟烷1ml",
                "硫酸阿托品注射液1ml:0.5mg","氯化钠注射液0.9%10ml","地塞米松磷酸钠注射液1ml:5mg","{}苯磺顺阿曲库铵注射液5ml：10mg","[欧贝针]盐酸昂丹司琼注射液4ml:8mg",
                "注射用盐酸瑞芬太尼1mg","葡萄糖氯化钠注射液5%250ml","氯化钠注射液0.9%100ml","★[希刻劳干混悬剂]头孢克洛干混悬剂0.125g*6","枸橼酸舒芬太尼注射液1ml:50ug",
                "[抗坏血酸针]维生素C注射液5ml:1g","西帕依固龈液30ml"
        };

        String[] array1 = new String[] {
                "苯磺酸氨氯地平片","阿司匹林肠溶片","坎地沙坦酯片","艾司唑仑片"
        };
        String result1 = "I10.x00";
        String disease1 = "I10.X02";


        String[] array2 = new String[] {
                "甘草酸二铵胶囊","溴隐停片"
        };
        String result2 = "B18.900,B17.900";
        String disease2 = "B18.951";

        String[] array3 = new String[] {
                "拉坦前列素滴眼剂"
        };
        String result3 = "";
        String disease3 = "I10.X02";

        String[] array4 = new String[] {
                "丙泊酚中","左氧氟沙星滴眼液","红霉素软膏","注射用头孢呋辛钠","盐酸氨溴索口服溶液","注射用矛头蝮蛇血凝酶","咪达唑仑注射液",
                "吸入用七氟烷","硫酸阿托品注射液","氯化钠注射液","地塞米松磷酸钠注射液","苯磺顺阿曲库铵注射液","盐酸昂丹司琼注射液","注射用盐酸瑞芬太尼",
                "葡萄糖氯化钠注射液","氯化钠注射液","头孢克洛干混悬剂","枸橼酸舒芬太尼注射液","维生素C注射液","西帕依固龈液"
        };
        String result4 = "L08,R58.x00,R11.x03,H40.500,H40.900,H40.100,H40.001,H40.8";
        String disease4 = "R11.X51";//now->R58.X02



        /*for (String name : nameArray) {
            handleTsMedicine2(name);
        }*/
        //handleTsMedicine2("[巴曲亭针]注射用矛头蝮蛇血凝酶2单位");
    }

    public static String handleTsMedicine2(String name) {
        if (null != name && !name.equalsIgnoreCase("")) {
            String commonName = name.replaceAll("[\\s\\.%]","");
            //特殊中括号转换
            commonName = commonName.replaceAll("【","[").replaceAll("】","]");
            commonName = commonName.replaceAll("［","[").replaceAll("］","]");
            //中括号
            Pattern pattern1 = Pattern.compile(".*\\[([^\\[\\]]*)\\].*");
            if (pattern1.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\[([^\\[\\]]*)\\]" , "");
            }
            //《基》﹛集﹜{颗粒} <级> 转换方便处理
            commonName = commonName.replaceAll("《","(").replaceAll("》",")");
            commonName = commonName.replaceAll("﹛","(").replaceAll("﹜",")");
            commonName = commonName.replaceAll("\\{","(").replaceAll("}",")");
            commonName = commonName.replaceAll("<","(").replaceAll(">",")");

            //基(甲)尿素乳膏 括号内容会在下面删除
            if (commonName.startsWith("基(")){
                commonName = commonName.replace("基","");
            }
            //小括号
            Pattern pattern2 = Pattern.compile(".*\\(([^\\(\\)]*)\\).*");
            if (pattern2.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\(([^\\(\\)]*)\\)" , "");
            }
            // ||123||
            Pattern pattern3 = Pattern.compile(".*\\|\\|([^\\|\\|\\|\\|]*)\\|\\|.*");
            if (pattern3.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\|\\|([^\\|\\|\\|\\|]*)\\|\\|" , "");
            }
            // [0-9]*/* 去除单位 100G/盒
            Pattern pattern4 = Pattern.compile(".*[0-9].*/.*");
            if (pattern4.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("[0-9].*/.*" , "");
            }
            // 去除单位  1mg*10
            Pattern pattern5_1 = Pattern.compile(".*[0-9]+[.]*[0-9]+[a-zA-Z]+\\*[0-9]+.*");
            if (pattern5_1.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("[0-9]+[.]*[0-9]+[a-zA-Z]+\\*[0-9]+" , "");
            }
            // 去除单位 100mg
            Pattern pattern5_2 = Pattern.compile(".*[0-9]+[a-zA-Z]+.*");
            if (pattern5_2.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("[0-9]+[a-zA-Z]+", "");
            }
            // /*
            Pattern pattern6_1 = Pattern.compile(".*\\/.*");
            if (pattern6_1.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\/.*" , "");
            }
            Pattern pattern6_2 = Pattern.compile(".*\\(.*");
            if (pattern6_2.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\(.*" , "");
            }
            Pattern pattern6_3 = Pattern.compile(".*\\|.*");
            if (pattern6_3.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("\\|.*" , "");
            }
            //去除药品名前的字母
            Pattern pattern7 = Pattern.compile("^[a-zA-Z]+.*");
            if (pattern7.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("^[a-zA-Z]+","");
            }
            //去除药品名后的字母
            Pattern pattern8 = Pattern.compile(".*[a-zA-Z]+$");
            if (pattern8.matcher(commonName).matches()) {
                commonName = commonName.replaceAll("[a-zA-Z]+$+","");
            }
            //颗在第一个字
            if (commonName.startsWith("颗")){
                commonName = commonName.replace("颗","");
            }
            int numIndex = findFirstDigitIndex(commonName);
            if (numIndex > 0){
                commonName = commonName.substring(0,numIndex);
            }
            commonName = commonName.replaceAll(regexPattern, "");
            System.out.println(name + " <revert> " + commonName);
            return commonName;
        }
        return name;
    }

    public static int findFirstDigitIndex(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                return i;
            }
        }
        return -1; // 如果没有找到数字，返回-1
    }

    public static void handleTsMedicine(String name){
        boolean flag;
        if (null != name && !name.equalsIgnoreCase("")){
            String commonName = name.replaceAll(" ","");
            flag = false;
            //如果原数据匹配不到药品，处理药品名称
            //第一步，中文括号替换匹配
            if (!flag && (commonName.contains("（") || commonName.contains("）"))) {
                commonName = commonName.replaceAll("（","(").replaceAll("）",")");
                System.out.println("1->" + commonName);
            }
            //第二步，去除"()" "-"外的其他特殊符号匹配
            if (!flag){
                String handleName = commonName.replaceAll(regexPattern , "");
                System.out.println("2->" + handleName);
            }
            //第三步，上一步如果匹配不到，括号及里面内容去掉
            if (!flag){
                //特殊中括号转换
                commonName = commonName.replaceAll("【","[").replaceAll("】","]");
                commonName = commonName.replaceAll("［","[").replaceAll("］","]");
                //中括号
                Pattern pattern = Pattern.compile(".*\\[([^\\[\\]]*)\\].*");
                if (pattern.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\[([^\\[\\]]*)\\]" , "");
                }
                //《基》﹛集﹜{颗粒} <级> 转换方便处理
                commonName = commonName.replaceAll("《","(").replaceAll("》",")");
                commonName = commonName.replaceAll("﹛","(").replaceAll("﹜",")");
                commonName = commonName.replaceAll("\\{","(").replaceAll("}",")");
                commonName = commonName.replaceAll("<","(").replaceAll(">",")");

                //基(甲)尿素乳膏 括号内容会在下面删除
                if (commonName.startsWith("基(")){
                    commonName = commonName.replace("基","");
                }

                //小括号
                Pattern pattern2 = Pattern.compile(".*\\(([^\\(\\)]*)\\).*");
                if (pattern2.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\(([^\\(\\)]*)\\)" , "");
                }
                // ||123||
                Pattern pattern3 = Pattern.compile(".*\\|\\|([^\\|\\|\\|\\|]*)\\|\\|.*");
                if (pattern3.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\|\\|([^\\|\\|\\|\\|]*)\\|\\|" , "");
                }
                // [0-9]*/* 去除单位 100G/盒
                Pattern pattern7 = Pattern.compile(".*[0-9].*/.*");
                if (pattern7.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("[0-9].*/.*" , "");
                }
                // 去除单位  1mg*10
                Pattern pattern8 = Pattern.compile(".*[0-9]+[.]*[0-9]+[a-zA-Z]+\\*[0-9]+.*");
                if (pattern8.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("[0-9]+[.]*[0-9]+[a-zA-Z]+\\*[0-9]+" , "");
                }
                // 去除单位 100mg
                Pattern pattern9 = Pattern.compile(".*[0-9]+[a-zA-Z]+.*");
                if (pattern9.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("[0-9]+[a-zA-Z]+" , "");
                }
                // /*
                Pattern pattern4 = Pattern.compile(".*\\/.*");
                if (pattern4.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\/.*" , "");
                }
                Pattern pattern5 = Pattern.compile(".*\\(.*");
                if (pattern5.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\(.*" , "");
                }
                Pattern pattern6 = Pattern.compile(".*\\|.*");
                if (pattern6.matcher(commonName).matches()) {
                    commonName = commonName.replaceAll("\\|.*" , "");
                }
                System.out.println("3->" + commonName);

                if (!flag){
                    //去除药品名前的字母
                    Pattern pattern10 = Pattern.compile("^[a-zA-Z]+.*");
                    if (pattern10.matcher(commonName).matches()) {
                        commonName = commonName.replaceAll("^[a-zA-Z]+","");
                    }
                    //去除药品名后的字母
                    Pattern pattern11 = Pattern.compile(".*[a-zA-Z]+$");
                    if (pattern11.matcher(commonName).matches()) {
                        commonName = commonName.replaceAll("[a-zA-Z]+$+","");
                    }

                    //颗在第一个字
                    if (commonName.startsWith("颗")){
                        commonName = commonName.replace("颗","");
                    }
                    System.out.println("4->" + commonName);
                    if (!flag){
                        //最后取第一个连续的中文，用以排除各种单位计量（ 非布司他片 40毫克*10片
                        Pattern pattern12 = Pattern.compile("^[^\\u4E00-\\u9FFF]*([\\u4e00-\\u9fa5]*).*$");
                        Matcher matcher = pattern12.matcher(commonName);
                        String patternName = commonName;
                        if (matcher.find()) {
                            patternName = matcher.group(1);
                        }
                        System.out.println("5->" + commonName);
                    }
                    commonName = commonName.replaceAll(regexPattern, "");
                    System.out.println("6->" + commonName);
                }
            }
        }
    }
}
